/* -----------------------------------------------------------------------------
 * PROJECT    : lwf-jaxb
 * FILE       : JaxbService.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 28, 2011  mycodetalks			       Created
 */
package net.lwf.jaxb.service;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import net.lwf.exception.ExceptionUtil;
import net.lwf.jaxb.util.XmlFileInfo;

/**
 * Class description goes here
 */
public abstract class JaxbService {
	
	private XmlFileInfo xmlFileInfo;
	
	public Object loadedXmlData() {
		loadXml();
		try {
			JAXBContext jc = JAXBContext.newInstance(getContextPath());
			Unmarshaller u = jc.createUnmarshaller();
			return u.unmarshal(this.xmlFileInfo.getInputStream());
		} catch (Exception ex) {
			ExceptionUtil.logAndThrowException(JaxbService.class, ex);
		}
		return null;
	}
	
	private synchronized void loadXml() {
		if (this.xmlFileInfo == null) {
			this.xmlFileInfo = new XmlFileInfo(getXmlFileName());
		} else {
			if (this.xmlFileInfo.isChanged() == false) {
				return;
			}
		}
	}

	public XmlFileInfo getXmlFileInfo() {
		return xmlFileInfo;
	}

	public abstract String getContextPath();

	public abstract String getXmlFileName();

}



